﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using Windows.Storage;

namespace CC98.Storage
{
    public static class SettingHelper
    {
        public static void WriteCookie(this Cookie cookie, ApplicationDataCompositeValue value)
        {
            if (cookie == null)
                throw new ArgumentNullException("cookie");

            value["comment"] = cookie.Comment;
            value["commentUri"] = cookie.CommentUri;
            value["discard"] = cookie.Discard;
            value["domain"] = cookie.Domain;
            value["expired"] = cookie.Expired;
            value["expires"] = cookie.Expires;
            value["httpOnly"] = cookie.HttpOnly;
            value["path"] = cookie.Path;
            value["port"] = cookie.Port;
            value["secure"] = cookie.Secure;
            value["value"] = cookie.Value;
            value["version"] = cookie.Version;
        }

        public static ApplicationDataCompositeValue WriteCookie(this Cookie cookie)
        {
            if (cookie == null)
                throw new ArgumentNullException("cookie");

            ApplicationDataCompositeValue value = new ApplicationDataCompositeValue();
            WriteCookie(cookie, value);
            return value;
        }

        public static Cookie ReadCookie(ApplicationDataCompositeValue value)
        {
            if (value == null)
                throw new ArgumentNullException("value");

            Cookie cookie = new Cookie();

            cookie.Comment = (string)value["comment"];
            cookie.CommentUri = (Uri)value["commentUri"];
            cookie.Discard = (bool)value["discard"];
            cookie.Domain = (string)value["domain"];
            cookie.Expired = (bool)value["expired"];
            cookie.Expires = (DateTime)value["expires"];
            cookie.HttpOnly = (bool)value["httpOnly"];
            cookie.Path = (string)value["path"];
            cookie.Port = (string)value["port"];
            cookie.Secure = (bool)value["secure"];
            cookie.Value = (string)value["value"];
            cookie.Version = (int)value["version"];

            return cookie;
        }
    }
}
